package com.sizu.web;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sizu.common.saledto.TCustomerReturnListDto;
import com.sizu.common.saledto.TSaleListDto;
import com.sizu.common.utils.BlandUtil;
import com.sizu.common.vo.ResultEntity;
import com.sizu.entity.*;
import com.sizu.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 赵玉玺
 * @since 2020-02-26
 */
@RestController
@RequestMapping("/sale/tCustomerReturnList")
public class TCustomerReturnListController {

    @Resource
    private ITCustomerReturnListService iTCustomerReturnListService;

    @Resource
    private ITCustomerReturnListGoodsService iTCustomerReturnListGoodsService;

    @Autowired
    private SyncService syncService;

    /**
     * 查询出售列表
     * @param tCustomerReturnListDto
     * @return
     */
    @RequestMapping("/list")
    public ResultEntity tCustomerReturnListList(TCustomerReturnListDto tCustomerReturnListDto){
        TCustomerResturnListZyx tCustomerResturnListZyx=new TCustomerResturnListZyx();
        BeanUtils.copyProperties(tCustomerReturnListDto,tCustomerResturnListZyx);
        List<TCustomerResturnListZyx> customerResturnListZyx = iTCustomerReturnListService.getCustomerResturnListZyx(tCustomerResturnListZyx);
        return ResultEntity.ok("查询成功",customerResturnListZyx);
    }

    /**
     * 添加方法
     * @param tCustomerReturnListDto
     * @return
     */
    @RequestMapping("/add")
    public ResultEntity tCustomerReturnListAdd(TCustomerReturnListDto tCustomerReturnListDto){

        TCustomerReturnList tCustomerReturnList=new TCustomerReturnList();
        BeanUtils.copyProperties(tCustomerReturnListDto,tCustomerReturnList);
        iTCustomerReturnListService.insert(tCustomerReturnList);
        tCustomerReturnListDto.setId(tCustomerReturnList.getId());
        syncService.syncCustomerreturn(tCustomerReturnListDto);
        //添加多的一方
        if(!StringUtils.isBlank(tCustomerReturnListDto.getTCustomerReturnListGoodsJson())){
            List<TCustomerReturnListGoods> tCustomerReturnListGoodss = JSON.parseArray(tCustomerReturnListDto.getTCustomerReturnListGoodsJson(), TCustomerReturnListGoods.class);
            for (TCustomerReturnListGoods tCustomerReturnListGoods : tCustomerReturnListGoodss) {
                tCustomerReturnListGoods.setCustomerReturnListId(tCustomerReturnList.getId());
                iTCustomerReturnListGoodsService.insert(tCustomerReturnListGoods);
            }
        }

        return ResultEntity.ok("添加成功");

    }

    /**
     * 修改方法
     * @param tCustomerReturnListDto
     * @return
     */
    @RequestMapping("/up")
    public ResultEntity tCustomerReturnListUp(TCustomerReturnListDto tCustomerReturnListDto){
        TCustomerReturnList tCustomerReturnList=new TCustomerReturnList();
        BeanUtils.copyProperties(tCustomerReturnListDto,tCustomerReturnList);
        iTCustomerReturnListService.updateById(tCustomerReturnList);
        //添加多的一方
        //添加多的一方
        if(!StringUtils.isBlank(tCustomerReturnListDto.getTCustomerReturnListGoodsJson())){
            Wrapper<TCustomerReturnListGoods> wrapper1=new EntityWrapper<>();
            wrapper1.eq("customer_return_list_id",tCustomerReturnListDto.getId());
            iTCustomerReturnListGoodsService.delete(wrapper1);
            Wrapper<TCustomerReturnList> wrapper=new EntityWrapper<>(tCustomerReturnList);
            TCustomerReturnList tCustomerReturnList1 = iTCustomerReturnListService.selectOne(wrapper);
            List<TCustomerReturnListGoods> tCustomerReturnListGoodss = JSON.parseArray(tCustomerReturnListDto.getTCustomerReturnListGoodsJson(), TCustomerReturnListGoods.class);
            for (TCustomerReturnListGoods tCustomerReturnListGoods : tCustomerReturnListGoodss) {
                tCustomerReturnListGoods.setCustomerReturnListId(tCustomerReturnList1.getId());
                iTCustomerReturnListGoodsService.insert(tCustomerReturnListGoods);
            }
        }
        return  ResultEntity.ok("修改成功");
    }

    /**
     * 删除方法
     * @param tCustomerReturnListDto
     * @return
     */
    @RequestMapping("/dels")
    public ResultEntity tCustomerReturnListDels(TCustomerReturnListDto tCustomerReturnListDto){
        for (Integer id : tCustomerReturnListDto.getIds()) {
            iTCustomerReturnListService.deleteById(id);
            Wrapper<TCustomerReturnListGoods> wrapper1=new EntityWrapper<>();
            wrapper1.eq("customer_return_list_id",tCustomerReturnListDto.getId());
            iTCustomerReturnListGoodsService.delete(wrapper1);
        }
        return ResultEntity.ok("删除成功");
    }

    /**
     * 返回随机编号
     * @return
     */
    @RequestMapping("/number")
    public ResultEntity getOrderNumber(){
        return ResultEntity.ok("获取成功","XT"+System.currentTimeMillis());
    }
}
